'''MainWindow'''
import sys

from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtWidgets import QApplication, QMainWindow
from PyQt5.QtCore import QTimer
from PyQt5.QtGui import QImage, QPixmap, QKeyEvent
from PyQt5.QtWidgets import QMessageBox,QInputDialog
from PyQt5.QtCore import Qt
from fatigue_algo.scrfd import scrfd
from fatigue_algo.landmark_mbv2.onnx.detector import Detector

from GUI.params.yawnSet import Ui_YawnSet_MainWindow

'''哈欠参数配置窗口'''
class YawnParamWindow(QtWidgets.QMainWindow, Ui_YawnSet_MainWindow):

    def __init__(self, cfg, preParamWindow = None, nextParamWindow = None, detectWindow = None, detect_handler = None):
        '''
        :param cfg: 疲劳检测算法的配置文件
        :param preParamWindow: 当前参数子界面的前一个页面
        :param nextParamWindow: 当前参数子界面的后一个页面
        :param detect_handler: 疲劳算法处理器
        '''
        super(YawnParamWindow,self).__init__()
        self.cfg = cfg
        self.setupUi(self)  # 初始化时创建窗口对象，但点击next时再显示
        self.init()  #按钮事件绑定
        self.preParamWindow = preParamWindow  #上一个参数子页面
        self.nextParamWindow = nextParamWindow  #下一个参数子页面
        self.handler = detect_handler  #疲劳算法处理器
        self.last_ParamWindow_Flag = False  # 最后一个参数配置子窗口的标志位

    #为按钮绑定enter槽函数
    def init(self):
        self.reset() #数据回显
        self.pushButton.clicked.connect(lambda : self.next())
        self.pushButton_3.clicked.connect(lambda : self.reset())

    # 设置"上一步"按钮可见
    def set_preButton_Visible(self):
        self.pushButton_2.setVisible(True)
        self.pushButton_2.clicked.connect(lambda: self.pre())

    #参数配置
    def paramSet(self):
        MAR_threshold = float(self.lineEdit.text()) # MAR阈值
        self.cfg["MAR_threshold"] = MAR_threshold

        FOM_threshold = float(self.lineEdit_2.text())
        self.cfg["FOM_threshold"] = FOM_threshold  # FOM阈值
        FOM_TIMEWINDOW_LENGTH = int(self.lineEdit_3.text())
        self.cfg["FOM_TIMEWINDOW_LENGTH"] = FOM_TIMEWINDOW_LENGTH  # FOM时窗长度

        # Yaw区间
        yaw1, yaw2 = float(self.lineEdit_4.text()), float(self.lineEdit_5.text())
        self.cfg["MAR_yaw_range"] = [yaw1,yaw2]
        # Yaw缩放因子
        beta1, beta2, beta3 = float(self.lineEdit_6.text()), float(self.lineEdit_7.text()), float(self.lineEdit_8.text())
        self.cfg["MAR_beta"] = [beta1, beta2, beta3]

    # 跳转至上一个参数配置窗口
    def pre(self):
        self.preParamWindow.cfg = self.cfg
        self.preParamWindow.show()  # 显示下一个参数配置子窗口
        self.close()

    # 跳转至下一个参数配置窗口
    def next(self):
        self.paramSet()
        self.handler.cfg = self.cfg  # 更新handler的cfg配置文件
        # 判断是否为终止界面
        if (not self.last_ParamWindow_Flag):
            self.nextParamWindow.cfg = self.cfg  # 传递cfg
            self.nextParamWindow.handler = self.handler  # 传递handler
            self.nextParamWindow.show()  # 显示下一个参数配置子窗口
            self.close()
        else:
            QMessageBox.information(self, "提示", "正在为你配置算法")
            self.close()
            self.handler.detect()
        # QMessageBox.warning(self, '警告', '请检查参数配置是否符合要求')

    # 重置默认参数
    def reset(self):
        # 参数回显
        self.lineEdit.setText(str(self.cfg["MAR_threshold"]))  # MAR阈值
        self.lineEdit_2.setText(str(self.cfg["FOM_threshold"]))  # FOM阈值
        self.lineEdit_3.setText(str(self.cfg["FOM_TIMEWINDOW_LENGTH"]))  # FOM时窗长度
        #Yaw区间
        self.lineEdit_4.setText(str(self.cfg["MAR_yaw_range"][0]))
        self.lineEdit_5.setText(str(self.cfg["MAR_yaw_range"][1]))
        #Yaw缩放因子
        self.lineEdit_6.setText(str(self.cfg["MAR_beta"][0]))
        self.lineEdit_7.setText(str(self.cfg["MAR_beta"][1]))
        self.lineEdit_8.setText(str(self.cfg["MAR_beta"][2]))